import 'dart:convert';
import 'package:dustbin_sort/model/dustbin_info.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:azlistview/azlistview.dart';
import 'package:lpinyin/lpinyin.dart';

class DustbinIndexDry extends StatefulWidget {
	@override
	State<StatefulWidget> createState() {
		return new _DustbinIndexDryState();
	}
}

class _DustbinIndexDryState extends State<DustbinIndexDry> {
	List<DustbinInfo> _DryDustbinList = List();

	
	
	
	List<DustbinInfo> _hotSearchList = List();

	int _suspensionHeight = 40;
	int _itemHeight = 50;
	String _suspensionTag = "";



	@override
	void initState() {
		super.initState();
		loadData();
	}

	void loadData() async {
		_hotSearchList.add(DustbinInfo(name: "塑料瓶", tagIndex: "★"));
		_hotSearchList.add(DustbinInfo(name: "纸盒", tagIndex: "★"));
		_hotSearchList.add(DustbinInfo(name: "一次性筷子", tagIndex: "★"));
		_hotSearchList.add(DustbinInfo(name: "一次性勺子", tagIndex: "★"));

		//加载城市列表
		rootBundle.loadString('assets/data/dry_dustbin.json').then((value) {
			Map dustbin_map = json.decode(value);
			for (int i = 0; i < dustbin_map.keys
					.toList()
					.length; i++) {
				_DryDustbinList.add(
						DustbinInfo(name: dustbin_map.keys.toList()[i]));
			}
			_handleList(_DryDustbinList);
			setState(() {
			});
		});
	}

	void _handleList(List<DustbinInfo> list) {
		if (list == null || list.isEmpty) return;
		for (int i = 0, length = list.length; i < length; i++) {
			String pinyin = PinyinHelper.getPinyinE(list[i].name);
			String tag = pinyin.substring(0, 1).toUpperCase();
			list[i].namePinyin = pinyin;
			if (RegExp("[A-Z]").hasMatch(tag)) {
				list[i].tagIndex = tag;
			} else {
				list[i].tagIndex = "#";
			}
		}
		//根据A-Z排序
		SuspensionUtil.sortListBySuspensionTag(_DryDustbinList);
	}

	void _onSusTagChanged(String tag) {
		setState(() {
			_suspensionTag = tag;
		});
	}

	Widget _buildSusWidget(String susTag) {
		susTag = (susTag == "★" ? "热门搜索" : susTag);
		return Container(
			height: _suspensionHeight.toDouble(),
			padding: const EdgeInsets.only(left: 15.0),
			color: Color(0xfff3f4f5),
			alignment: Alignment.centerLeft,
			child: Text(
				'$susTag',
				softWrap: false,
				style: TextStyle(
					fontSize: 14.0,
					color: Color(0xff999999),
				),
			),
		);
	}

	Widget _buildListItem(DustbinInfo model) {
		String susTag = model.getSuspensionTag();
		susTag = (susTag == "★" ? "热门搜索" : susTag);
		return Column(
			children: <Widget>[
				Offstage(
					offstage: model.isShowSuspension != true,
					child: _buildSusWidget(susTag),
				),
				SizedBox(
					height: _itemHeight.toDouble(),
					child: ListTile(
						title: Text(model.name),
						onTap: () {
							print("OnItemClick: $model");
							Navigator.pop(context, model);
						},
					),
				)
			],
		);
	}

	@override
	Widget build(BuildContext context) {
		print(_DryDustbinList.length);
		return Scaffold(
			appBar: AppBar(
				title: Text(
					"干垃圾",
					style: TextStyle(color: Colors.black,fontWeight: FontWeight.w300,),
				),
				backgroundColor: Colors.white,
				elevation: 0,
				leading: new IconButton(icon: Icon(Icons.arrow_back, color: Colors.black,), onPressed: (){
					Navigator.of(context).pop();
				}),
			),
			body: Column(
				children: <Widget>[
					Container(
						alignment: Alignment.centerLeft,
						padding: const EdgeInsets.only(left: 15.0),
						height: 50.0,
						child: Text("热门搜索 筷子"),
					),
					Expanded(
							flex: 1,
							child: AzListView(
								data: _DryDustbinList,
								topData: _hotSearchList,
								itemBuilder: (context, model) => _buildListItem(model),
								suspensionWidget: _buildSusWidget(_suspensionTag),
								isUseRealIndex: true,
								itemHeight: _itemHeight,
								suspensionHeight: _suspensionHeight,
								onSusTagChanged: _onSusTagChanged,
								//showCenterTip: false,
							)),
				],
			),
		);
	}
}


